﻿using DevComponents.AdvTree;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.ManageUtility.DataAccess;
using PlanningGIS.ManageUtility;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Manager
{
    /// <summary>
    /// 部门树
    /// </summary>
    public partial class DepartmentTree : AdvTree
    {
        /// <summary>
        /// 
        /// </summary>
        public DepartmentTree()
        {
            InitializeComponent();
            SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
                 System.Windows.Forms.ControlStyles.ResizeRedraw | System.Windows.Forms.ControlStyles.UserPaint, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        public DepartmentTree(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
                 System.Windows.Forms.ControlStyles.ResizeRedraw | System.Windows.Forms.ControlStyles.UserPaint, true);
        }

        /// <summary>
        /// 
        /// </summary>
        private List<Department> Departments = null;
        /// <summary>
        /// 
        /// </summary>
        public void BindData()
        {
            try
            {
                this.Nodes.Clear();
                Departments = ManageHelper.DBContext.GetAllEntry<Department>();
                if (Departments == null || Departments.Count == 0) return;
                AddNode("", null);
                this.ExpandAll();
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentid"></param>
        /// <param name="parentnode"></param>
        private void AddNode(string parentid, Node parentnode)
        {
            List<Department> nds = Departments.FindAll(t => parentid.Equals(t.Parent_ID, StringComparison.CurrentCultureIgnoreCase));
            if (nds == null || nds.Count == 0) return;
            foreach (Department d in nds)
            {
                Node n = AddNode(d, parentnode);
                AddNode(d.ID, n);
            }
        }

        /// <summary>
        /// 在指定的节点上添加节点
        /// </summary>
        /// <param name="department">要添加的资源节点</param>
        /// <param name="parentNode">父节点</param>
        public Node AddNode(Department department, Node parentNode)
        {
            Node n = new Node();
            n.Name = department.ID;
            n.Text = department.Name;
            n.Tag = department;
            if (parentNode == null)
            {
                this.Nodes.Add(n);
            }
            else
            {
                parentNode.Nodes.Add(n);
            }
            return n;
        }


    }
}
